package de.koller.pulsar.util;

import de.koller.oceanFramework.math.IProjection;
import de.koller.oceanFramework.math.RVec3;

public class InterceptPerspective implements IProjection {
	
	private final double width, height;
	private final double nearPlane;
	
	public static InterceptPerspective create( double fovY, double width, double height ) {
		return new InterceptPerspective( fovY, width, height );
	}
	
	private InterceptPerspective( double fovY, double width, double height ) {
		nearPlane = (height / 2.) / Math.tan( Math.toRadians( fovY ) / 2. );
		this.width = width;
		this.height = height;
	}

	@Override
	public RVec3 project( RVec3 v, RVec3 result ) {
		if( result == null ) result = new RVec3();
	
		result.x = (float)( nearPlane / (nearPlane + v.z) * v.x + width/2. );
		result.y = (float)( nearPlane / (nearPlane + v.z) * v.y + height/2. );
		result.z = v.z;
		
		return result;
	}

	@Override
	public RVec3 unproject( RVec3 v, RVec3 result ) {
		throw new UnsupportedOperationException( "Not supported yet." );
	}
	
	
}
